# coding=utf-8
"""
作者：dnscmd@qq.com
创建时间：2025/2/10
描述：
"""
import os
import chardet

def detect_encoding(file_path, sample_size=1024):
    """检测文件的编码"""
    with open(file_path, 'rb') as f:
        raw_data = f.read(sample_size)
        result = chardet.detect(raw_data)
        return result['encoding']

def convert_to_utf8(directory, overwrite=False):
    """
    批量转换目录中的 TXT 文件为 UTF-8

    :param directory: 需要转换的文件夹路径
    :param overwrite: 是否覆盖原文件，默认 False（不覆盖，生成新文件）
    """
    for filename in os.listdir(directory):
        if filename.endswith(".txt"):
            file_path = os.path.join(directory, filename)
            new_file_path = file_path if overwrite else f"{file_path}.utf8"

            try:
                # 先检测文件编码
                encoding = detect_encoding(file_path)
                if not encoding:
                    print(f"无法检测 {filename} 的编码，跳过")
                    continue

                # 读取原文件内容并转换
                with open(file_path, 'r', encoding=encoding) as f:
                    content = f.read()

                # 以 UTF-8 保存
                with open(new_file_path, 'w', encoding='utf-8') as f:
                    f.write(content)

                print(f"转换成功: {filename} (原编码: {encoding}) -> {new_file_path}")

            except Exception as e:
                print(f"转换失败: {filename}，错误: {e}")




if __name__ == '__main__':
    # 示例用法（修改为你的文件夹路径）
    convert_to_utf8("D:\\llmlibs", overwrite=True)
